/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.enterprise.model.Material;
import org.crystalrange.enterprise.service.MaterialService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class MaterialListController extends BaseController {

    protected MaterialService materialService;
    protected Material selectedMaterial;
    protected Listbox listboxMaterial;
    protected Search searchMaterial;
    protected List<Material> materials;

    public MaterialService getMaterialService() {
        return this.materialService;
    }

    public void setMaterialService(MaterialService materialService) {
        this.materialService = materialService;
    }

    public Material getSelectedMaterial() {
        return this.selectedMaterial;
    }

    public void setSelectedMaterial(Material selectedMaterial) {
        this.selectedMaterial = selectedMaterial;
    }

    public List<Material> getMaterials() {
        return this.materials;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchMaterial = new Search(Material.class);
        this.searchMaterial.setMaxResults(100);
        this.searchMaterial.addFetch("createdBy");
        this.searchMaterial.addFetch("updatedBy");
        this.materials = this.materialService.search(this.searchMaterial);

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemMaterialCreate(Event event) throws Exception {
        this.materialCreate();
    }

    public void onClick$menuitemMaterialEdit(Event event) throws Exception {
        this.materialEdit();
    }

    public void onClick$menuitemMaterialDelete(Event event) throws Exception {
        this.materialDelete();
    }

    public void onClick$menuitemMaterialSearch(Event event) throws Exception {
        this.materialSearch();
    }

    public void onClick$menuitemMaterialRefresh(Event event) throws Exception {
        this.materialRefresh();
    }

    public void onClick$toolbarbuttonMaterialCreate(Event event) throws Exception {
        this.materialCreate();
    }

    public void onClick$toolbarbuttonMaterialEdit(Event event) throws Exception {
        this.materialEdit();
    }

    public void onClick$toolbarbuttonMaterialDelete(Event event) throws Exception {
        this.materialDelete();
    }

    public void onClick$toolbarbuttonMaterialSearch(Event event) throws Exception {
        this.materialSearch();
    }

    public void onClick$toolbarbuttonMaterialRefresh(Event event) throws Exception {
        this.materialRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.materialEdit();
    }
    
    private void materialCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/material.zul");
    }

    private void materialEdit() throws Exception {
        if (this.selectedMaterial != null) {
            this.execution.sendRedirect("~./zul/html/material.zul?Id=" + this.selectedMaterial.getId().toString());
        }
    }

    private void materialDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxMaterial.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    Material material = (Material) selectedItem.getValue();
                    this.materialService.remove(material);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.materials = this.materialService.search(this.searchMaterial);
        this.binder.loadAll();
    }

    private void materialSearch() throws Exception {
        this.execution.setAttribute("searchMaterial", this.searchMaterial);

        Window window = (Window) this.execution.createComponents("~./zul/html/materialSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchMaterial = (Search) this.execution.getAttribute("searchMaterial");
        }

        this.materials = this.materialService.search(this.searchMaterial);
        this.binder.loadAll();
    }

    private void materialRefresh() throws Exception {
        this.materials = this.materialService.search(this.searchMaterial);
        this.binder.loadAll();
    }
}
